1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30 import java.util.concurrent.atomic.*;
31 import java.util.*;
32
33 public class Lazy {
34 volatile int ii;
35 volatile long ll;
36 volatile Boolean bb;
37 static final Lazy z = new Lazy();
38
39 public static void main(String[] args) throws Exception {
40 final AtomicBoolean b = new AtomicBoolean();
41 final AtomicInteger i = new AtomicInteger();
42 final AtomicLong l = new AtomicLong();
43 final AtomicReference<Long> r = new AtomicReference<Long>();
44
45 final AtomicIntegerArray ia = new AtomicIntegerArray(1);
46 final AtomicLongArray la = new AtomicLongArray(1);
47 final AtomicReferenceArray<Long> ra = new AtomicReferenceArray<Long>(1);
48
49 final AtomicIntegerFieldUpdater<Lazy> iu =
50 AtomicIntegerFieldUpdater.newUpdater(Lazy.class, "ii");
51 final AtomicLongFieldUpdater<Lazy> lu =
52 AtomicLongFieldUpdater.newUpdater(Lazy.class, "ll");
53 final AtomicReferenceFieldUpdater<Lazy,Boolean> ru =
54 AtomicReferenceFieldUpdater.newUpdater(Lazy.class,
55 Boolean.class, "bb");
56
57 Thread[] threads = {
58 new Thread() { public void run() { b.lazySet(true); }},
59 new Thread() { public void run() { i.lazySet(2); }},
60 new Thread() { public void run() { l.lazySet(3L); }},
61 new Thread() { public void run() { r.lazySet(9L); }},
62 new Thread() { public void run() { ia.lazySet(0,4); }},
63 new Thread() { public void run() { la.lazySet(0,5L); }},
64 new Thread() { public void run() { ra.lazySet(0,6L); }},
65 new Thread() { public void run() { iu.lazySet(z,7); }},
66 new Thread() { public void run() { lu.lazySet(z,8L); }},
67 new Thread() { public void run() { ru.lazySet(z,true); }}};
68
69 for (Thread t : threads) t.start();
70 for (Thread t : threads) t.join();
71
72 if (! (b.get() == true &&
73 i.get() == 2 &&
74 l.get() == 3L &&
75 r.get() == 9L &&
76 ia.get(0) == 4 &&
77 la.get(0) == 5L &&
78 ra.get(0) == 6L &&
79 z.ii == 7 &&
80 z.ll == 8L &&
81 z.bb == true))
82 throw new Exception("lazySet failed");
83 }
84 }